package net.tatu.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.tatu.dao.SysLogMapper;
import net.tatu.dao.UserMapper;
import net.tatu.model.PubCode;
import net.tatu.model.SysLog;
import net.tatu.model.User;
import net.tatu.service.PubCodeService;
import net.tatu.service.SysLogService;
import net.tatu.service.UserService;
import net.tatu.util.Encrypt;
import net.tatu.util.Util;

@Service
public class SysLogServiceImpl implements SysLogService {
	Logger log = Logger.getLogger(SysLogServiceImpl.class);
	@Autowired
	private SysLogMapper sysLogMapper;
	
	@Autowired
	private PubCodeService pubCodeService;
	
	@Override
	public int insert(SysLog sysLog) {
		log.debug("log insert to database:" + sysLog.toString());
		int u = sysLogMapper.insert(sysLog);
		log.debug("log insert success:" + u);
		if(u != 1){
			log.error("日志记录失败:" + sysLog.toString());
		}
		return u;
	}

	@Override
	public int newLog(int logTypeCode, int eventStatusCode, int logKindCode,
			long objectId) {
		SysLog sysLog = new SysLog();
		sysLog.setLogTypeCode(logTypeCode);
		sysLog.setEventStatusCode(eventStatusCode);
		sysLog.setLogKindCode(logKindCode);
		sysLog.setObjectId(objectId);
		sysLog.setRecordStatusCode(1);
		String contents = "";
		PubCode pubCode = new PubCode(logKindCode);
		String logKind = pubCodeService.getPubCodeDesc(pubCode);
		pubCode.setCodeId(logTypeCode);
		String logType = pubCodeService.getPubCodeDesc(pubCode);
		pubCode.setCodeId(eventStatusCode);
		String eventStatus = pubCodeService.getPubCodeDesc(pubCode);
		contents = logKind + ":" + logType + ", " + objectId + " ," + eventStatus;
		sysLog.setLogContents(contents);
		return insert(sysLog);
	}
}
